package com.shop.shoppingmall.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shop.shoppingmall.dao.ShBackUserDao;
import com.shop.shoppingmall.dao.ShMerchantDao;
import com.shop.shoppingmall.entity.ShBackUser;
import com.shop.shoppingmall.entity.ShMerchant;
import com.shop.shoppingmall.service.ShMerchantService;
import com.shop.shoppingmall.util.JwtUtil;
import com.shop.shoppingmall.util.api.vo.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * (ShMerchant)表控制层
 *
 * @author makejava
 * @since 2020-09-09 14:53:50
 */
@RestController
@RequestMapping("shMerchant")
@CrossOrigin
public class ShMerchantController {
    /**
     * 服务对象
     */
    @Resource
    private ShMerchantService shMerchantService;
    @Resource
    private ShBackUserDao shBackUserDao;
    @Resource
    private ShMerchantDao shMerchantDao;

    /**
     * 通过主键查询单条数据
     *
     * @param
     * @return 单条数据
     */
    @PostMapping("/selectOne")
    public ShMerchant selectOne(@RequestBody Map map) {
        String id = (String) map.get("id");
        return this.shMerchantService.queryById(id);
    }

    /**
     * 根据条件查询所有店铺信息
     * @param map
     * @return
     */
    @PostMapping("listAll")
    public Result listAll(@RequestBody Map map){
        return shMerchantService.listAll(map);
    }

    /**
     * 添加商铺
     * @param jsonObject
     * @return
     */
    @PostMapping("/insertMerchant")
    public Result insertMerchant(HttpServletRequest request,@RequestBody JSONObject jsonObject) {
        //解析token，拿到userId
//        String name = JwtUtil.getUserNameByToken(request);
//        ShBackUser shBackUser = shBackUserDao.selectLoginName(name);
//        if(StringUtils.isBlank(name) || null == shBackUser){
//            return Result.error("请先注册或者登录！");
//        }
        ShMerchant shMerchant = JSON.parseObject(jsonObject.toJSONString(), ShMerchant.class);
        if(shMerchant.getUserId() == null){
            return Result.error("用户主键不能为空");
        }
        List<ShMerchant> shMerchantList = shMerchantDao.selectByUserId(shMerchant.getUserId());
        if(shMerchantList.size()>0){
            return shMerchantService.update(shMerchant);
        }
        return shMerchantService.insert(shMerchant);
    }

    /**
     * 修改店铺信息
     * @param jsonObject
     * @return
     */
    @PostMapping("/updateMerchant")
    public Result updateMerchant(@RequestBody JSONObject jsonObject) {
        ShMerchant shMerchant = JSON.parseObject(jsonObject.toJSONString(), ShMerchant.class);
        return shMerchantService.update(shMerchant);
    }

    @PostMapping("/selectMerchant")
    public Result selectUserMerchant(@RequestBody JSONObject jsonObject) {
      String userId = jsonObject.get("userId").toString();
      if(userId ==null){
          return Result.error("店长主键不能为空");
      }
        return shMerchantService.selectUserId(userId);
    }

}
